package com.magold.game.factory;

import com.magold.io.XMLReader;
import com.magold.level.ILevel;
import com.magold.logic.ISpriteLogic;
import com.magold.logic.region.MagneticRegion;
import com.magold.physics.magnetic.Magnetic;
import com.magold.physics.magnetic.MagneticPole;

public class MagneticRegionFactory extends BaseLogicFactory {
	private ILevel mCurrentLevel;
	
	public MagneticRegionFactory() {
		super(MagneticRegion.class.getSimpleName());
	}

	@Override
	public synchronized boolean onLoadEntity(XMLReader pReader, ILevel pLoadingLevel) {
		mCurrentLevel = pLoadingLevel;
		return super.onLoadEntity(pReader, pLoadingLevel);
	}

	@Override
	protected ISpriteLogic createSafely(XMLReader pReader) {
		float pWidth = pReader.getFloat("width");
		
		float pHeight = pReader.getFloat("height");
		
		char topFlag = pReader.getString("top_pole").charAt(0);
		MagneticPole pTopPole = MagneticPole.forFlag(topFlag);
		
		char bottomFlag = pReader.getString("bottom_pole").charAt(0);
		MagneticPole pBottomPole = MagneticPole.forFlag(bottomFlag);
		
		int pSourceID = pReader.getInt("source_id");
		Magnetic pSource = (Magnetic) mCurrentLevel.getIdentifiedObject(pSourceID);
		
		MagneticRegion r = new MagneticRegion(pWidth, pHeight, pTopPole, pBottomPole, pSource);
		
		return r;
	}

}
